<template>
  <el-dialog
    :title="!dataForm.seckillId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="秒杀名称" prop="seckillName">
      <el-input v-model="dataForm.seckillName" placeholder="秒杀名称"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="秒杀描述" prop="describes">
      <el-input v-model="dataForm.describes" placeholder="秒杀描述"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="num">
      <el-input v-model="dataForm.num" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="订单时间" prop="cancel">
      <el-input v-model="dataForm.cancel" placeholder="订单时间"></el-input>
    </el-form-item>
    <el-form-item label="是否开启" prop="seckillWhether">
      <el-input v-model="dataForm.seckillWhether" placeholder="是否开启"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          seckillId: 0,
          seckillName: '',
          createTime: '',
          endTime: '',
          describes: '',
          num: '',
          cancel: '',
          seckillWhether: ''
        },
        dataRule: {
          seckillName: [
            { required: true, message: '秒杀名称不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          describes: [
            { required: true, message: '秒杀描述不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          cancel: [
            { required: true, message: '订单时间不能为空', trigger: 'blur' }
          ],
          seckillWhether: [
            { required: true, message: '是否开启不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.seckillId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.seckillId) {
            this.$http({
              url: this.$http.adornUrl(`/product/activitysurface/info/${this.dataForm.seckillId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.seckillName = data.activitySurface.seckillName
                this.dataForm.createTime = data.activitySurface.createTime
                this.dataForm.endTime = data.activitySurface.endTime
                this.dataForm.describes = data.activitySurface.describes
                this.dataForm.num = data.activitySurface.num
                this.dataForm.cancel = data.activitySurface.cancel
                this.dataForm.seckillWhether = data.activitySurface.seckillWhether
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/activitysurface/${!this.dataForm.seckillId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seckillId': this.dataForm.seckillId || undefined,
                'seckillName': this.dataForm.seckillName,
                'createTime': this.dataForm.createTime,
                'endTime': this.dataForm.endTime,
                'describes': this.dataForm.describes,
                'num': this.dataForm.num,
                'cancel': this.dataForm.cancel,
                'seckillWhether': this.dataForm.seckillWhether
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
